import { Page_Prefab, UI_Layer } from "../../config/ui_def"

cc.Class({
    extends: cc.Component,

    properties: {
        uiName: {
            visible: false,
            default: '',
        },
        loadCount: {
            default: 0,
            visible: false
        },
        // 页面打开次数
        openTime: {
            default: 0,
            visible: false
        },
        layer: {
            visible: false,
            default: UI_Layer.Base,
            type: cc.Enum(UI_Layer),
        },
        page_node: {
            visible: false,
            default: null,
            type: cc.Node
        },           // page节点
        background_node: cc.Node,     // 黑底节点
        close_buttons: [cc.Button],   // 关闭按钮列表
    },
    /**
    * @description 初始化面板
    * @param {Object} info 
    * @param {String} info.name 必填 面板信息
    * @param {String} info.page   面板使用page目录下其他的预制体。一般默认 同面板信息同名
    * @param {String} info.url    面板使用其他目录预制体。一般默认 同面板信息同名
    * @param {String} info.script 面板使用其他的脚本。一般默认 同面板信息同名
    * @param {String} info.updatePage  面板底部层
    * @param {String} info.cover_opacity 面板底部层颜色
    * @param {String} info.close  面板底部点击可以关闭
    * @param {String} info.alignment 首帧对齐
    * 
    *
    */
    initInfo(info) {
        this.info = info
        this.uiName = info.name     // 面板信息
        this.node.name = info.name  // 节点信息
        this.layer = info.layer
        this.node.setContentSize(gg.uiManager.canvasSize)
        this.checkUibase()
        if (this.checkPrefabs()) {
            this.loadOver()
        }
    },
    // 关闭面板 若对应脚本存在
    closePage() {
        if (!this.node.active) {
            return
        }
        if (gg.uiManager.pageDict[this.info.name + '_' + this.layer] && gg.uiManager.pageDict[this.info.name + '_' + this.layer].closeUI) {
            gg.uiManager.pageDict[this.info.name + '_' + this.layer].closeUI()
        }
        this.node.active = false
    },
    // 其他检测项 比如背景 和 关闭按钮
    checkUibase() {
        this.updateCover()          // 检查遮罩层
        this.updateCloseButton()    // 关闭按钮
    },
    // 加载页面预制体
    checkPrefabs() {
        let checkover = true
        // true为直接打开 false为需要加载
        let pageurl = 'page/'
        if (this.info.url) {
            pageurl = this.info.url
        } else if (this.info.page) {
            pageurl += this.info.page
        } else {
            pageurl += this.uiName
        }
        if (he.load.get(pageurl, cc.Prefab)) {
            this.loadCount += 1
        } else {
            he.load.loadRes(pageurl, cc.Prefab, this.addLoadCount.bind(this))
            checkover = false
        }
        this.startTime = new Date();
        if (Page_Prefab[this.uiName] && Page_Prefab[this.uiName].length) {
            for (let i = 0; i < Page_Prefab[this.uiName].length; i++) {
                let info = {}
                if (typeof Page_Prefab[this.uiName][i] === 'string') {
                    info.name = Page_Prefab[this.uiName][i]
                    info.type = cc.Prefab
                } else {
                    info = Page_Prefab[this.uiName][i]
                }
                let loadres = he.load.get(info.name, info.type)
                if (loadres) {
                    this.loadCount += 1
                } else {
                    if (checkover) {
                        checkover = false
                        gg.uiManager.switchLoad(true)
                    }
                    // he.log(info)
                    he.load.loadRes(info.name, info.type, this.addLoadCount.bind(this), info.bundle)
                }
            }
        }
        return checkover
    },
    // 计数增加
    addLoadCount(err, asset) {
        if (asset) {
            this.loadCount += 1
            if (Page_Prefab[this.uiName]) {
                if (Page_Prefab[this.uiName].length + 1 == this.loadCount) {
                    this.loadOver()
                }
            } else {
                this.loadOver()
            }
        }
    },
    // 加载完成
    loadOver() {
        if (!this.info) {
            return
        }
        // console.log(new Date() - this.startTime, `    ${this.uiName}`)
        let pageNode = this.node.getChildByName(this.uiName)
        // cc.log(pageNode,this.uiName)
        if (!pageNode) {
            let pageurl = 'page/'
            if (this.info.url) {
                pageurl = this.info.url
            } else if (this.info.page) {
                pageurl += this.info.page
            } else {
                pageurl += this.uiName
            }
            pageNode = cc.instantiate(he.load.get(pageurl, cc.Prefab))
            pageNode.name = this.uiName
            pageNode.scale = zoom_scale
            this.node.insertChild(pageNode, 2)
        }
        let pageScript
        if (this.info.script) {
            pageScript = pageNode.getComponent(this.info.script)
        } else {
            pageScript = pageNode.getComponent(this.uiName)
        }
        if (pageScript) {
            gg.uiManager.pageDict[this.info.name + '_' + this.layer] = pageScript
            pageScript.uiBase = this

            if (this.info && this.info.alignment && pageNode.getComponent(cc.Widget)) {
                let pageWidget = pageNode.getComponent(cc.Widget)
                pageWidget.updateAlignment()
            }
            if (!this.openTime) {
                pageScript.initPage(this.info)
            }
            pageScript.updatePage(this.info)
        } else {
            he.warn(this.uiName, "没有绑定脚本 脚本名称：", this.info.script)
        }
        gg.uiManager.switchLoad(false)
        if (!this.node) {
            return
        }
        this.node.active = true
        this.page_node = pageNode
        this.openTime += 1
    },
    // 给关闭按钮绑定事件
    updateCloseButton() {
        if (this.info.close) {
            for (let i = 0; i < this.close_buttons.length; i++) {
                this.close_buttons[i].interactable = this.info.close
                var clickEventHandler = new cc.Component.EventHandler();
                clickEventHandler.target = this.node;
                clickEventHandler.component = "uiBase";// 这个是代码文件名
                clickEventHandler.handler = "closePage";
                this.close_buttons[i].clickEvents[0] = clickEventHandler
            }
        } else {
            for (let i = 0; i < this.close_buttons.length; i++) {
                this.close_buttons[i].interactable = false
            }
        }
    },
    // 按钮状态调整
    updateCloseButtonStatus(status) {
        for (let i = 0; i < this.close_buttons.length; i++) {
            this.close_buttons[i].interactable = status
        }
    },
    // 更新覆盖
    updateCover() {
        if (this.info.cover) {
            this.background_node.active = this.info.cover
            if (this.info.cover_opacity >= 0) {
                this.background_node.opacity = this.info.cover_opacity
            } else {
                this.background_node.opacity = 150
            }
        } else {
            this.background_node.active = false
        }
    },
});
